(define-module (jlicht build xpi-utils)
  #:use-module (ice-9 popen)
  #:use-module (guix build json)
  #:use-module (srfi srfi-26)
  #:export (read-manifest-json
            multi-assoc-ref
            extract-id))

(define (read-manifest-json file)
  (let* ((cmd (string-append "unzip -p \"" file "\" manifest.json"))
         (port (open-input-pipe cmd))
         (str  (read-json port)))
    (close-pipe port) str))

(define (multi-assoc-ref alist . keys)
  (if (or (null? keys))
      alist
      (let* ((v (assoc-ref alist (car keys))))
        (apply multi-assoc-ref v (cdr keys)))))

(define (extract-id al)
  (multi-assoc-ref al
                   "browser_specific_settings"
                   "gecko"
                   "id"))
